package com.powerunion.datacollection.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.DaoException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.dao.GlobalTargetsDataDao;
import com.powerunion.datacollection.model.GlobalTargetsData;
import com.powerunion.datacollection.model.jsonObject.GlobalTargetsDataJSONObject;
import com.powerunion.datacollection.model.jsonObject.JSONDataBean;
import com.powerunion.util.log.LogHelper;

public class GlobalTargetsDataManager {
	private static Logger logger = LogHelper.getInstance().getLogger(
			GlobalTargetsDataManager.class);
	private GlobalTargetsDataDao dao = null;

	public GlobalTargetsDataManager() {
		super();
		this.dao = (GlobalTargetsDataDao) DaoFactory.getInstance().getDao(
				GlobalTargetsDataDao.class);
	}

	public boolean save(String saveStr,String documentId,String submiUser,String procId,int iTemplate,String actId)
	 throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(saveStr)) {
			String[] args = { "saveStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		// 参数为空
		if (StringUtils.isEmpty(documentId)) {
			String[] args = { "documentId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter documentId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args); 
		}

		Map gMap = new HashMap();
		gMap.put("data", GlobalTargetsDataJSONObject.class);
		gMap.put("dataArray", GlobalTargetsData.class);
		JSONDataBean am = (JSONDataBean)JSONObject.toBean(JSONObject.fromObject(saveStr), JSONDataBean.class,gMap);
		List list = am.getData();
		boolean flag = false;
		if(list != null && !list.isEmpty()){
			flag = this.dao.save(list, documentId, submiUser, procId, iTemplate,actId); 
		}
		if(flag){
			//更新对应的初始化指标报送表单状态为已被用，将不能更改该表单的初始化数据
			flag = this.dao.updateFormState(documentId);
		}
		
		return flag;
	}
	
	public boolean save(String saveStr,String documentId,int permissionUPFile)throws Exception {
		// 参数为空
		if (StringUtils.isEmpty(saveStr)) {
			String[] args = { "saveStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		// 参数为空
		if (StringUtils.isEmpty(documentId)) {
			String[] args = { "documentId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter documentId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		//先删除原先已有的数据
		this.dao.delete(documentId);
		Map gMap = new HashMap();
		gMap.put("data", GlobalTargetsDataJSONObject.class);
		gMap.put("dataArray", GlobalTargetsData.class);
		JSONDataBean am = (JSONDataBean)JSONObject.toBean(JSONObject.fromObject(saveStr), JSONDataBean.class,gMap);
		List list = am.getData();
		boolean flag = true;
		if(!list.isEmpty()){
			flag = this.dao.save(list, documentId,permissionUPFile);
		}
		
		return flag;
	}
	
	public List getTemplateData(String documentId) throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(documentId)) {
			String[] args = { "documentId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter documentId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.getTemplateData(documentId);
	}
	
	public boolean save(List globalTargetsDataList, String formId,int permissionUPFile)throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.save(globalTargetsDataList, formId,permissionUPFile);
	}
	
	public List getTargetSubmiData(String documentId,String submiUser,String procId,String actId) throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(documentId)) {
			String[] args = { "documentId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter documentId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		// 参数为空
		if (StringUtils.isEmpty(submiUser)) {
			String[] args = { "submiUser" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter submiUser NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		// 参数为空
		if (StringUtils.isEmpty(procId)) {
			String[] args = { "procId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter procId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		// 参数为空
		if (StringUtils.isEmpty(actId)) {
			String[] args = { "actId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter actId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getTargetSubmiData(documentId, submiUser, procId, actId);
	}
	
	public List getApproveActivityData(String documentId,String procId,String actId) throws Exception{
		// 参数为空
		if (StringUtils.isEmpty(documentId)) {
			String[] args = { "documentId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter documentId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}

		// 参数为空
		if (StringUtils.isEmpty(procId)) {
			String[] args = { "procId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter procId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		// 参数为空
		if (StringUtils.isEmpty(actId)) {
			String[] args = { "actId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter actId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getApproveActivityData(documentId, procId, actId);
	}
	
	public int getPermissionUPFile(String formId) throws DaoException {
		// 参数为空
		if (StringUtils.isEmpty(formId)) {
			String[] args = { "formId" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter formId NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		return this.dao.getPermissionUPFile(formId);
	}
}
